package dq.study.concurrent.semaphore;

/**
 * @author dongqun
 * @version 1.0
 * @description 类的功能描述
 * @date 2019/9/5 19:12
 */
public class SemaphoreTest {

    public static void main(String[] args) throws InterruptedException {

        //模拟场景1- 二进制信号量
//        SemaphoreService semaphoreService = new SemaphoreService();
//        for (int i =0;i<10;i++){
//            MyThread myThread = new MyThread("thread-"+(i+1),semaphoreService);
//            myThread.start();
//        }

        //模拟场景2- 多许可
//        SemaphoreService semaphoreService1 = new SemaphoreService();
//        for (int i =0;i<10;i++){
//            MyThread myThread = new MyThread("thread-"+(i+1),semaphoreService1);
//            myThread.start();
//        }


        //模拟场景3 - 方法 acquire( int permits ) 参数作用，及动态添加 permits 许可数量
//        SemaphoreService2 semaphoreService = new SemaphoreService2();
//        MyThread myThread;
//        for (int i =0;i<10;i++){
//            myThread = new MyThread("thread-"+(i+1),semaphoreService);
//            myThread.start();
//            //Thread.sleep(1000);
//            System.out.println("剩余许可数"+semaphoreService.availablePermits());
//        }

        //模拟场景4 - 方法 tryAcquire() 、 tryAcquire(int permits)、 tryAcquire(int permits , long timeout , TimeUint unit) 的使用
        SemaphoreService3 semaphoreService = new SemaphoreService3();
        MyThread myThread;
        for (int i =0;i<10;i++){
            myThread = new MyThread("thread-"+(i+1),semaphoreService);
            myThread.start();
        }


        //模拟场景5 -多进程多处理1
//        for (int i =0;i<10;i++){
//            SemaphoreService5 semaphoreService = new SemaphoreService5();
//            MyThread myThread = new MyThread("thread-"+(i+1),semaphoreService);
//            myThread.start();
//        }


        //模拟场景6 -多进程多处理2
//        for (int i =0;i<10;i++){
//            SemaphoreService4 semaphoreService = new SemaphoreService4();
//            MyThread myThread = new MyThread("thread-"+(i+1),semaphoreService);
//            myThread.start();
//        }
    }
}
